<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>14.冒泡事件改进渐变轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            list-style-type: none;
        }
        
        .lunbo {
            width: 1226px;
            height: 460px;
            margin: 0 auto;
            position: relative;
        }
        
        .lunbo>.imgLists {
            width: 100%;
            height: 100%;
        }
        
        .lunbo>.imgLists>.imgItem {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition: all 1s;
        }
        
        .lunbo>.imgLists>.imgItem.active {
            opacity: 1;
        }
        
        .lunbo>.imgLists>.imgItem>img {
            width: 100%;
            height: 100%;
        }
        
        .lunbo>.preBtn {
            width: 40px;
            height: 40px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: calc(50% - 20px);
            left: 0;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
        }
        
        .lunbo>.nextBtn {
            width: 40px;
            height: 40px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: calc(50% - 20px);
            right: 0;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
        }
        
        .lunbo>.pots {
            width: 300px;
            height: 40px;
            position: absolute;
            left: calc(80% - 150px);
            bottom: 10%;
        }
        
        .lunbo>.pots>.pot {
            width: 15px;
            height: 15px;
            margin: 0 15px;
            float: left;
            border-radius: 10px;
            font-size: 15px;
            line-height: 15px;
            text-align: center;
            background-color: #fff;
        }
    </style>
</head>

<body>
    <div class="lunbo">
        <div class="imgLists">
            <div class="imgItem active"><img src="../img/mi1.jpg" alt=""></div>
            <div class="imgItem"><img src="../img/mi2.webp" alt=""></div>
            <div class="imgItem"><img src="../img/mi3.webp" alt=""></div>
            <div class="imgItem"><img src="../img/mi4.webp" alt=""></div>
            <div class="imgItem"><img src="../img/mi5.webp" alt=""></div>
        </div>
        <div class="preBtn">
            <</div>
                <div class="nextBtn">></div>
                <!--轮播图下面五个点-->
                <ul class="pots">
                    <li class="pot" style="background-color:red" id="circle0">1</li>
                    <li class="pot" id="circle1">2</li>
                    <li class="pot" id="circle2">3</li>
                    <li class="pot" id="circle3">4</li>
                    <li class="pot" id="circle4">5</li>
                </ul>
        </div>
        <script>
            var lunbo = document.querySelector(".lunbo");
            var preBtn = document.querySelector(".preBtn");
            var nextBtn = document.querySelector(".nextBtn");
            var imgItem = document.querySelectorAll(".imgItem");
            var pot = document.querySelectorAll(".pot");
            var pots = document.querySelector(".pots");
            //一。左点击时间
            var currentImg = 0; //记录当前照片第几张
            preBtn.onclick = function(e) {
                    //console.log(e);
                    currentImg--;
                    if (currentImg < 0) {
                        currentImg = imgItem.length - 1;
                    }
                    renderImg();
                }
                //二。右点击事件
            nextBtn.onclick = function(e) {
                //console.log(e);
                currentImg++;
                if (currentImg > imgItem.length - 1) {
                    currentImg = 0;
                }
                renderImg();
            }

            //封装渲染时间

            function renderImg() {
                //1.先将所有图片的active都出掉
                imgItem.forEach(function(item, index) {
                    //console.log(item + index);
                    item.classList.remove("active");

                })
                pot.forEach(function(item, index) {
                    //console.log(item);
                    item.style.backgroundColor = "#fff";
                });
                //2.讲下一个图片添加上active
                imgItem[currentImg].classList.add("active");
                pot[currentImg].style.backgroundColor = "red";
            }

            //小圆点点击事件
            //冒泡事件解决小圆点点击事件
            pots.onclick = function(event) {
                //console.log(event);
                /* 
                通过冒泡事件将事件传递到父元素，然后通过筛选出需要的子元素，这样就避免了循环和遍历
                */
                if (event.target.classList.contains("pot")) {
                    //console.log(parseInt(event.target.id[6]));
                    var index = parseInt(event.target.id[6])
                    currentImg = index;
                    renderImg();
                }
            }
        </script>
</body>

</html>